package mn.idax.exchange.otc.interceptor;


import com.alibaba.fastjson.JSON;
import mn.idax.exchange.common.I18nLanguageUtil;
import mn.idax.exchange.common.UTCTimeUtil;
import mn.idax.exchange.otc.enums.LogCategoryEnum;
import mn.idax.exchange.otc.enums.LogLevelEnum;
import mn.idax.exchange.otc.util.JsonUtil;
import mn.idax.exchange.otc.util.LogUtils;
import mn.idax.exchange.otc.util.UUidUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;

/**
 * Created by wei on 2017/5/10 0010.
 * 注解将一个java类定义为切面类
 */
@Aspect
@Configuration
@EnableAutoConfiguration
public class LogMethodInterceptor {

    /**
     * 方法拦截器打印Controller INFO日志
     *
     * @param proceedingJoinPoint
     * @return
     * @throws Throwable
     */
    @Around("execution(public * mn.idax.exchange.otc.web..*.*(..))")
    public Object doControllerLog(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //返回参数
        Object returnVal = proceedingJoinPoint.proceed();
        String returnJson = JsonUtil.writeValue(returnVal);

        //1、流水号
        String serilNo = UTCTimeUtil.getUTCTimeStringOne() + UUidUtils.getUUID();
        StringBuffer requestParam = new StringBuffer();
        try {
            String methodName = proceedingJoinPoint.getSignature().getDeclaringTypeName() + " method:" + proceedingJoinPoint.getSignature().getName();
            //请求参数
            Object[] args = proceedingJoinPoint.getArgs();
            if (args.length > 0) {
                //打印请求日志
                LogUtils.unifLog(this.getClass(), LogLevelEnum.INFO.getValue(),methodName,serilNo,LogCategoryEnum.REQUEST.getValue(), JSON.toJSONString(args[0]));
            }
            //打印返回日志
            LogUtils.unifLog(this.getClass(), LogLevelEnum.INFO.getValue(),methodName,serilNo,LogCategoryEnum.RESPONSE.getValue(), JSON.toJSONString(returnVal));
        } catch (Exception e) {


        }
        //返回国际化语言
        return I18nLanguageUtil.replaceLangeage(returnJson);
    }


    /**
     * 方法拦截器打印Service INFO日志
     *
     * @param proceedingJoinPoint
     * @return
     * @AfterReturning(pointcut="execution(* mn.idax.exchange.marketapi.service..*.*(..))",returning="returnValue")
     */
    public Object doServiceLog(JoinPoint proceedingJoinPoint, Object returnValue) {
        //返回参数
        Object returnVal = returnValue;
        //1、流水号
        String serilNo = UTCTimeUtil.getUTCTimeStringOne() + UUidUtils.getUUID();
        StringBuffer requestParam = new StringBuffer();
        try {
            String methodName = proceedingJoinPoint.getSignature().getDeclaringTypeName() + " method:" + proceedingJoinPoint.getSignature().getName();
            //请求参数
            Object[] args = proceedingJoinPoint.getArgs();
            if (args.length > 0) {
                //打印请求日志
                LogUtils.unifLog(this.getClass(), LogLevelEnum.DEBUG.getValue(), methodName, serilNo, LogCategoryEnum.REQUEST.getValue(), JSON.toJSONString(args[0]));
            }
            //打印返回日志
            LogUtils.unifLog(this.getClass(), LogLevelEnum.DEBUG.getValue(), methodName, serilNo, LogCategoryEnum.RESPONSE.getValue(), JSON.toJSONString(returnVal));
        } catch (Exception e) {

        }
        return returnVal;
    }

}
